﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars.Docking;
using Story.Library;
using System.Reflection;
using System.Threading;

namespace Story.Framework.WinUI
{
    public partial class DockPanelBase : UserControl
    {
        public DockPanelBase()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            if (this.DesignMode) return;
            RefreshButtonClick();
            base.OnLoad(e);
        }

        private string m_Title;

        [Category("基础"),Description("设置DockPanel显示的标题")]
        public string Title
        {
            get { return m_Title; }
            set { m_Title = value; }
        }
        private DockingStyle m_DockingStyle;
        [Category("基础"),Description("设置DockPanel显示的方式")]
        public DockingStyle XDockingStyle
        {
            get { return m_DockingStyle; }
            set { m_DockingStyle = value; }
        }

        public static DockPanelBase Create(string typeInfoStr, string paramsStr)
        {
            string typeName = typeInfoStr.Split(',')[0].Trim();
            string assemblyname = string.Format("{0}{1}.dll", ReflectionHelper.ExecutingPath, typeInfoStr.Split(',')[1].Trim());
            Assembly assembly = Assembly.LoadFrom(assemblyname);
            DockPanelBase dp = null;
            if (!string.IsNullOrEmpty(paramsStr))
            {
                Dictionary<string, string> dic = StringHelper.GetKeyValuePairString(paramsStr);
                Type t = assembly.GetType(typeName);
                dp = (DockPanelBase)Activator.CreateInstance(t, new object[] { dic });
                if (dic.ContainsKey("Title"))
                    dp.Title = dic["Title"];

            }
            else
                dp = (DockPanelBase)assembly.CreateInstance(typeName);
            return dp;
        }
        public virtual void SendMsageDo(string key, string msg)
        {
        }
        /// <summary>
        /// 刷新按钮的基本实现，可被重写，但一般情况下不应该重写此方法
        /// 主要使用多线程方式调用了RefreshData方法，并禁用刷新按钮和显示“正在载入”的状态信息
        /// </summary>
        public virtual void RefreshButtonClick()
        {
            Thread t = new Thread(new ThreadStart(RefreData));
          
            t.Start();

        }
        /// <summary>
        /// 刷新（载入）数据的基本实现
        /// 应该重写此方法来达到真正的数据载入和逻辑显示，并且在载入成功之后调用 base.RefreData()
        /// </summary>
        public virtual void RefreData()
        {
            if (this.InvokeRequired)
            {
                ClientDelegate d = DoRefreData;
                this.BeginInvoke(d);
            }
            else
            {
                DoRefreData();
            }
        }
        private void DoRefreData()
        {

        }

    }
}
